//
//  FisrtTableViewController.m
//  ICup
//
//  Created by YouQiang on 14/11/27.
//  Copyright (c) 2014年 Echo. All rights reserved.
//

#import "FisrtTableViewController.h"
#import "AppDelegate.h"
#import "singlePeripheral.h"

@interface FisrtTableViewController (){
    NSMutableArray *array;
    NSMutableDictionary *dic;
    BOOL isConnecting;
}
@end

@implementation FisrtTableViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    
    array = [[NSMutableArray alloc] init];
    dic = [[NSMutableDictionary alloc] init];
    
    self.title = @"选择设备";
    [singlePeripheral instance];
    self.navigationItem.leftBarButtonItem = nil;
    
    [NC addObserver:self selector:@selector(searchdeviceName:) name:NOTI_DEVICE_NAME object:nil];
    [NC addObserver:self selector:@selector(connectName:) name:NOTI_DEVICE_CONNECTED object:nil];
}
-(void)connectName:(NSNotification*)noti{
    CBPeripheral *curperipheral = noti.object;
    for (CBPeripheral *peripheral in array) {
        if ([[peripheral.identifier UUIDString] isEqualToString:[curperipheral.identifier UUIDString]]) {
            [self enter];
            break;
        }
    }
}
-(void)searchdeviceName:(NSNotification*)noti{
    CBPeripheral *peripheral = noti.object;
    
    if ([peripheral.identifier UUIDString]) {
        if (![dic objectForKey:[peripheral.identifier UUIDString]]) {
            [dic setObject:[peripheral.identifier UUIDString] forKey:[peripheral.identifier UUIDString]];
            if ([[peripheral.name lowercaseString] isEqualToString:@"icup"]) {
                [array addObject:peripheral];
                [self.tableView reloadData];
            }
        }
    }
}
-(void)dealloc{
    [NC removeObserver:self];
}
#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 60;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return  array.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableview cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *CellIdentifier = @"SettingBaseCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:nil];
        cell.selectionStyle = UITableViewCellSelectionStyleGray;
    }
    CBPeripheral *peripheral = [array objectAtIndex:indexPath.row];
    cell.textLabel.text = peripheral.name;
    cell.detailTextLabel.text = [peripheral.identifier UUIDString];
    cell.backgroundColor = [UIColor clearColor];//ios7必须要这样处理，不然默认会是白色
    return cell;
}
#pragma mark - Table view delegate

- (void)tableView:(UITableView *)atableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [atableView deselectRowAtIndexPath:indexPath animated:YES];
    if (!isConnecting) {
        CBPeripheral *peripheral = [array objectAtIndex:indexPath.row];
        [[singlePeripheral instance] connectDevice:peripheral];
        isConnecting = YES;
    }
}
-(void)enter{
    [[AppDelegate instance] performSelector:@selector(enterMainUI2) withObject:nil afterDelay:.4];

}

@end
